package com.ylr.basic.framework.controller.web;

import com.ylr.basic.framework.service.AesService;
import com.ylr.base.common.tool.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.Objects;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-04-21 14:36:45
 * className: AesWebController AES加解密web请求接口类
 * version: 1.0
 * description:
 */
@Validated
@RestController
@RequestMapping("/web/aes")
public class AesWebController {

    private static final Logger log = LoggerFactory.getLogger(AesWebController.class);

    @Resource(name = "aesService")
    private AesService aesService;

    /**
     * AES密文解密接口
     * <p>
     * 调用方：
     *  1、
     *
     * @param cipherText 密文
     * @return 明文
     */
    @PostMapping("/decode/hex")
    public Result<String> decodeHex(@NotBlank(message = "密文不能为空") @RequestParam("cipherText") String cipherText) {
        log.info("web => 开始AES密文（十六进制）解密操作。cipherText={}", cipherText);

        String plainText = aesService.decodeHex(cipherText);

        log.info("web => AES密文（十六进制）解密操作完成");
        return Result.success(plainText);
    }

    /**
     * AES密文半解密接口
     * @param cipherText 密文
     * @param start      脱敏开始下标
     * @param end        脱敏结束下标
     * @return 明文
     */
    @PostMapping("/sub/decode/hex")
    public Result<String> subDecodeHex(@NotBlank(message = "密文不能为空") @RequestParam("cipherText") String cipherText,
                                       @RequestParam(value = "start", required = false) Integer start,
                                       @RequestParam(value = "end", required = false) Integer end) {
        log.info("web => 开始AES密文（十六进制）半解密操作。 cipherText={} start={} end={}", cipherText, start, end);
        if (Objects.isNull(start)) {
            start = 3;
        }
        if (Objects.isNull(end)) {
            end = 4;
        }
        String plainText = aesService.subDecodeHex(cipherText, start, end);

        log.info("web => AES密文（十六进制）半解密操作完成");
        return Result.success(plainText);
    }

    /**
     * AES密文解密接口
     * <p>
     * 调用方：
     *  1、
     *
     * @param cipherText 密文
     * @return 明文
     */
    @PostMapping("/decode/base64")
    public Result<String> decodeBase64(@NotBlank(message = "密文不能为空") @RequestParam("cipherText") String cipherText) {
        log.info("web => 开始AES密文（Base64）解密操作。cipherText={}", cipherText);

        String plainText = aesService.decodeBase64(cipherText);

        log.info("web => AES密文（Base64）解密操作完成");
        return Result.success(plainText);
    }

    /**
     * AES密文半解密接口
     * @param cipherText 密文
     * @return 明文
     */
    @PostMapping("/sub/decode/base64")
    public Result<String> subDecodeBase64(@NotBlank(message = "密文不能为空") @RequestParam("cipherText") String cipherText,
                                          @RequestParam(value = "start", required = false) Integer start,
                                          @RequestParam(value = "end", required = false) Integer end) {
        log.info("web => 开始AES密文（Base64）半解密操作。cipherText={} start={} end={}", cipherText, start, end);
        if (Objects.isNull(start)) {
            start = 3;
        }
        if (Objects.isNull(end)) {
            end = 4;
        }

        String plainText = aesService.subDecodeBase64(cipherText, start, end);

        log.info("web => AES密文（Base64）半解密操作完成");
        return Result.success(plainText);
    }


}
